package com.hevs.samplewebapp.client.bean;

import javax.validation.ValidationException;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * <p>
 * FieldVerifier validates that the name the user enters is valid.
 * </p>
 * <p>
 * This class is in the <code>shared</code> packing because we don't use it
 * on the server. On the client, we verify different values before sending an RPC
 * request so the user doesn't have to wait for a network round trip to get feedback. 
 * </p>
 * <p>
 * When creating a class that is used on both the client and the server, be sure
 * that all code is translatable and does not use native JavaScript. Code that
 * is note translatable (such as code that interacts with a database or the file
 * system) cannot be compiled into client side JavaScript. Code that uses native
 * JavaScript (such as Widgets) cannot be run on the server.
 * </p>
 */
public class FieldVerifier
{
	/**
	 * Verifies that the specified integer is valid for our service.
	 *
	 * In this example, the integer should be between the minimum and maximum. The interval is given
	 * to the method. It's equal greater than the minimum and lower equal the maximum. If this is not
	 * the case, an error message is shown. This also occurs if the value is a string and not an
	 * integer.
	 *
	 * @param name the name referenced to the value to check
	 * @param field the text-box with the integer in it to check
	 * @param minInclusive integer which sets the minimum of the interval
	 * @param maxInclusive integer which sets the maximum of the interval
	 * @throws ValidationException is thrown if the value neither is an integer nor in the interval
	 * @return true if valid, false if invalid
	 */
	public static int validateAndParseInt(String name, TextBox field, int minInclusive, int maxInclusive) throws ValidationException{
		int value = 0;
		// error message string
		String errorMessage = name + " must be a whole number between " + minInclusive + " and " + maxInclusive + ".";
		try {
			//check if the value isn't empty
			if(field.getValue()!=""){
				//try to get an integer out of the text-box
				value = new Integer(field.getText());
				//check if the value is in the given interval
				if ( value < minInclusive || value > maxInclusive) {
					throw new ValidationException(errorMessage);
				}
			}
		} catch(NumberFormatException e) {
			throw new ValidationException(errorMessage);
		}                               
		return value;
	}
	/**
	 * Verifies that the specified double is valid for our service.
	 *
	 * In this example, the double should be between the minimum and maximum. The interval is given
	 * to the method. It's greater than the minimum and lower than the maximum. If this is not
	 * the case, an error message is shown. This also occurs if the value is a string and not a
	 * double.
	 *
	 * @param name the name referenced to the value to check
	 * @param field the text-box with the double in it to check
	 * @param min double which sets the minimum of the interval
	 * @param max double which sets the maximum of the interval
	 * @throws ValidationException is thrown if the value neither is a double nor in the interval
	 * @return true if valid, false if invalid
	 */
	public static double validateAndParseDouble(String name,TextBox field,double min,double max) throws ValidationException
	{
		double value=0.;
		// error string message
		String errorMessage=name+" must be a decimal number between "+min+" and "+max+".";
		try
		{
			//check if value isn't empty
			if(field.getValue()!="")
			{
				//try to parse the field value into a double
				value=new Double(field.getText());
				//check if the value is in the given interval
				if(value<min|value>max)
					throw new ValidationException(errorMessage);
			}
		}
		catch(NumberFormatException e)
		{
			throw new ValidationException(errorMessage);
		}
		return value;
	}
	/**
	 * Verifies that the a specified category is selected if necessary.
	 *
	 * In this example, we check if a value is entered and that a category is selected. The list-box
	 * has the different categories. Some values has to be referred to a category.
	 *
	 * @param name the name referenced to the value to check
	 * @param field the text-box with the value in it to check
	 * @param list list of the different categories
	 * @throws ValidationException is thrown if the value is empty or no / wrong category is selected
	 */
	public static void validateCategory(String name,TextBox field,ListBox list) throws ValidationException
	{
		//error message
		String errorMessage=name+" needs a specified category.";
		try
		{
			//check if the value ins't empty and
			//a category other than the first, which is "NONE", is selected
			if(field.getValue()!=""&&(list.getSelectedIndex()==0||list.getSelectedIndex()==-1))
				throw new ValidationException(errorMessage);
		}
		catch(Exception e)
		{
			throw new ValidationException(errorMessage);
		}
	}
}
